const BaseController = require("./base")
class UserOrBookController extends BaseController {
  async Success(result, msg) {
    const { ctx } = this
    ctx.body = {
      code: 200,
      data: {
        code: 200,
        msg: msg,
        data: result,
      },
    }
  }
  async Error(msg) {
    const { ctx } = this
    ctx.body = {
      code: 5000,
      data: {
        code: 5000,
        msg,
      },
    }
  }
  //为用户添加图书记录
  async addUserBook() {
    const { ctx, service } = this
    let data = ctx.request.body
    const isExists = await service.userorbook.isExist(data)
    if (isExists.length > 0) {
      this.Success(data,'已关联')
    } else {
      let result = await service.userorbook.addUserBook(data)
      if (result) {
        this.Success(result,'关联成功')
      }else{
        this.Error('关联失败')
      }
    }
  }
  //获取用户阅览过的图书记录
  async getUserOrBook() {
    const { ctx, service } = this
    let id = ctx.params.id
    let result = await service.userorbook.getUserBook(id)
    if (result) {
      this.Success(result,'获取成功')
    }else{
      this.Error('获取失败')
    }
  }
}
module.exports = UserOrBookController
